package com.yun.space.pro.mainPicture.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.yun.space.base.BaseAction;
import com.yun.space.entity.MainPicture;

/**
 * 轮播图
 * @author guo
 *
 */
public class MainPictureAction extends BaseAction<MainPicture>{
	private File file;//文件
	private String fileName;//文件名称 
	private String fileContentType; //文件类型 
	private String savePath;//文件名
	//进入轮播图修改页面
	public String toEditTopRotationMap(){
		MainPicture mainPicture = mainPictureService.getById(getModel().getId());
		ActionContext.getContext().getValueStack().set("mainPicture", mainPicture);
		return "toEditTopRotationMap";
	}
	//轮播图修改页面
	public String editTopRotationMap() throws Exception{
		try {
			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileName());
			FileInputStream fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//保存文件的文件名到数据库
		getModel().setPicture(getFileName());
		mainPictureService.update(getModel());
		//获得所有的首页轮播图
		ActionContext.getContext().getValueStack().set("mainPictures",mainPictureService.findAll());
		return "editTopRotationMap";
	}


	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
    public String getSavePath() throws Exception {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

}
